package pool;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolDemo1 {
    //循环10万个线程，创建线程池，执行任务
    public static final CountDownLatch latch = new CountDownLatch(100000);
    public static void main(String[] args) {
        //创建线程池对象
        ExecutorService threadPool = Executors.newFixedThreadPool(10);
        long start = System.currentTimeMillis();//计算耗时
        for (int i = 0; i < 100000; i++) {
            threadPool.submit(new Runnable() {//创建任务对象
                @Override
                public void run() {
                    latch.countDown();
                }
            });
        }
        try {
            latch.await();//等待所有任务执行完毕
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("耗时："+(end-start));
        threadPool.shutdown();//关闭线程池
    }
}
